package calculadora;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;


public class Tela extends JFrame {

	private double valor1 = 0.0, valor2 = 0.0;
	private int contaponto = 0;
	private String operacao;
	private JPanel contentPane;
	private JTextField txtResu;
	private JButton btn0;
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	private JButton btn5;
	private JButton btn6;
	private JButton btn7;
	private JButton btn8;
	private JButton btn9;
	private JButton btndividir;
	private JButton btnigual;
	private JButton btnmais;
	private JButton btnmenos;
	private JButton btnvirgula;
	private JButton btnvezes;
	private JButton btnLimpar;
	private JButton btnRaiz;
	private JLabel lblNome;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tela frame = new Tela();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Tela() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("H:\\icon.fw.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 474, 340);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtResu = new JTextField();
		txtResu.setHorizontalAlignment(SwingConstants.RIGHT);
		txtResu.setText("0");
		txtResu.setBounds(32, 60, 387, 31);
		contentPane.add(txtResu);
		txtResu.setColumns(10);
		
		btn0 = new JButton("0");
		btn0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn0.getText());
				
			}
		});
		btn0.setBounds(10, 265, 122, 31);
		contentPane.add(btn0);
		
		btn1 = new JButton("1");
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn1.getText());
			}
		});
		btn1.setBounds(10, 223, 56, 31);
		contentPane.add(btn1);
		
		btn2 = new JButton("2");
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn2.getText());
			}
		});
		btn2.setBounds(76, 223, 56, 31);
		contentPane.add(btn2);
		
		btn3 = new JButton("3");
		btn3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn3.getText());
			}
		});
		btn3.setBounds(142, 223, 56, 31);
		contentPane.add(btn3);
		
		btn4 = new JButton("4");
		btn4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn4.getText());
			}
		});
		btn4.setBounds(10, 181, 56, 31);
		contentPane.add(btn4);
		
		btn5 = new JButton("5");
		btn5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn5.getText());
			}
		});
		btn5.setBounds(76, 181, 56, 31);
		contentPane.add(btn5);
		
		btn6 = new JButton("6");
		btn6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn6.getText());
			}
		});
		btn6.setBounds(142, 181, 56, 31);
		contentPane.add(btn6);
		
		btn7 = new JButton("7");
		btn7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn7.getText());
			}
		});
		btn7.setBounds(10, 137, 56, 31);
		contentPane.add(btn7);
		
		btn8 = new JButton("8");
		btn8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn8.getText());
			}
		});
		btn8.setBounds(76, 137, 56, 31);
		contentPane.add(btn8);
		
		btn9 = new JButton("9");
		btn9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText(txtResu.getText()+btn9.getText());
			}
		});
		btn9.setBounds(142, 137, 56, 31);
		contentPane.add(btn9);
		
		btndividir = new JButton("/");
		btndividir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtResu.getText());
				operacao = btndividir.getText();
				lblNome.setText(txtResu.getText()+operacao);
				txtResu.setText("0");
				contaponto=0;
				}
		});
		btndividir.setBounds(288, 102, 70, 54);
		contentPane.add(btndividir);
		
		btnigual = new JButton("=");
		btnigual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor2 = Double.valueOf(txtResu.getText());
				if(operacao.equals("+"))
					txtResu.setText(
							String.valueOf(valor1+valor2)
							);
				else if(operacao.equals("-"))
					txtResu.setText(
							String.valueOf(valor1-valor2)
							);
				else if(operacao.equals("/"))
					txtResu.setText(
							String.valueOf(valor1/valor2)
							);
				else if(operacao.equals("*"))
					txtResu.setText(
							String.valueOf(valor1*valor2)
							);
				else if(operacao.equals("\u221A"))
								txtResu.setText(
										String.valueOf(Math.sqrt(valor1)));
				lblNome.setText(lblNome.getText()+valor2+btnigual.getText()+txtResu.getText());
				}
			}
		);
		btnigual.setForeground(new Color(0, 0, 0));
		btnigual.setBounds(386, 102, 70, 200);
		contentPane.add(btnigual);
		
		btnmais = new JButton("+");
		btnmais.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtResu.getText());
				operacao = btnmais.getText();
				lblNome.setText(txtResu.getText()+operacao);
				txtResu.setText("0");
				contaponto=0;
			}
		});
		btnmais.setBounds(208, 102, 70, 54);
		contentPane.add(btnmais);
		
		btnmenos = new JButton("-");
		btnmenos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtResu.getText());
				operacao = btnmenos.getText();
				lblNome.setText(txtResu.getText()+operacao);
				txtResu.setText("0");
				contaponto=0;
			}
	});
				
		btnmenos.setBounds(208, 175, 70, 54);
		contentPane.add(btnmenos);
		
		btnvirgula = new JButton(".");
		btnvirgula.addActionListener(new ActionListener() {
					
			public void actionPerformed(ActionEvent e) {
				if(contaponto!=1){
					contaponto=1;
				txtResu.setText(txtResu.getText()+btnvirgula.getText());
			}				}
		});
		btnvirgula.setBounds(142, 265, 56, 31);
		contentPane.add(btnvirgula);
		
		btnvezes = new JButton("*");
		btnvezes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtResu.getText());
				operacao = btnvezes.getText();
				lblNome.setText(txtResu.getText()+operacao);
				txtResu.setText("0");
				contaponto=0;
				
			}
	});
		btnvezes.setBounds(208, 242, 70, 54);
		contentPane.add(btnvezes);
		
		btnLimpar = new JButton("Limpar");
		
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResu.setText("0");
				valor1=valor2=0.0;
				contaponto=0;
				lblNome.setText("Petúnia (A Calculadora do Matheus)");
			}
		});
		btnLimpar.setBounds(10, 103, 188, 23);
		contentPane.add(btnLimpar);
		
		btnRaiz = new JButton("\u221A");
		btnRaiz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				valor1 = Double.valueOf(txtResu.getText());
				operacao = btnRaiz.getText();
				lblNome.setText(operacao + txtResu.getText());
				contaponto=0;
				txtResu.setText(
						String.valueOf(Math.sqrt(valor1)));
				
			}
	});
		btnRaiz.setBounds(288, 175, 70, 54);
		contentPane.add(btnRaiz);
		
		lblNome = new JLabel("Petúnia (A Calculadora do Matheus)");
		lblNome.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 20));
		lblNome.setBounds(32, 11, 387, 38);
		contentPane.add(lblNome);
		
	}
}
